#ifndef _SCHEDULER_H_
#define _SCHEDULER_H_

#include <queue>
#include <utility>

#include "functor.hpp"

namespace Medved
{

class Scheduler
{
public:
	typedef Functor<void> Action;

	struct Value
	{
		Value(int t, const Action &a) :
			time(t), act(a)
		{}

		int time;
		Action act;
		bool operator <(const Value& rhs) const { return time > rhs.time; }
	};

	Scheduler() :
		time_(0)
	{}

	void setTime(int time) { time_ = time; }

	void schedule(int time, const Action &act)
	{
		q_.push(Value(time + time_, act));
	}

	void timeStep()
	{
		time_++;
		while (!q_.empty() && q_.top().time <= time_) {
			q_.top().act();
			q_.pop();
		}
	}

private:
	std::priority_queue<Value> q_;
	int time_;
};

}

#endif
